package com.ruoyi.trash.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import java.io.InputStream;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.trash.domain.Trash;
import com.ruoyi.trash.service.ITrashService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回收站Controller
 *
 * @author huacai
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/trash/trash")
public class TrashController extends BaseController
{
    @Autowired
    private ITrashService trashService;

    /**
     * 查询回收站列表
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:list')")
    @GetMapping("/list")
    public TableDataInfo list(Trash trash)
    {
        startPage();
        List<Trash> list = trashService.selectTrashList(trash);
        return getDataTable(list);
    }

    /**
     * 导出回收站列表
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:export')")
    @Log(title = "回收站", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Trash trash)
    {
        List<Trash> list = trashService.selectTrashList(trash);
        ExcelUtil<Trash> util = new ExcelUtil<Trash>(Trash.class);
        util.exportExcel(response, list, "回收站数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Trash> util = new ExcelUtil<Trash>(Trash.class);
        util.importTemplateExcel(response, "回收站数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "回收站", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('trash:trash:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Trash> util = new ExcelUtil<Trash>(Trash.class);
        InputStream inputStream = file.getInputStream();
        List<Trash> list = util.importExcel(inputStream );
        inputStream.close();
        int count = trashService.batchInsertTrash(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取回收站详细信息
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:query')")
    @GetMapping(value = "/{trashId}")
    public AjaxResult getInfo(@PathVariable("trashId") Integer trashId)
    {
        return success(trashService.selectTrashByTrashId(trashId));
    }

    /**
     * 新增回收站
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:add')")
    @Log(title = "回收站", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Trash trash)
    {
        return toAjax(trashService.insertTrash(trash));
    }

    /**
     * 修改回收站
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:edit')")
    @Log(title = "回收站", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Trash trash)
    {
        return toAjax(trashService.updateTrash(trash));
    }

    /**
     * 删除回收站
     */
    @PreAuthorize("@ss.hasPermi('trash:trash:remove')")
    @Log(title = "回收站", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trashIds}")
    public AjaxResult remove(@PathVariable Integer[] trashIds)
    {
        return toAjax(trashService.deleteTrashByTrashIds(trashIds));
    }
}
